/*!
    \file    console_debug.c
    \brief   通用console调试打印接口 
    \version V2.0.0
	\Copyright (c) 2023,JW Technology Co., Ltd. 
*/

#include "console_debug.h"

#pragma module_name = "?__write"



void xprintf_putchar( uint8_t ch)  
{
	UartX_Put_Char(DEBUG_COM, ch);
}


int MyLowLevelPutchar(int x)
{
    UartX_Put_Char(DEBUG_COM, (uint8_t) x);
    return x;
}


/*
* If the __write implementation uses internal buffering, uncomment
* the following line to ensure that we are called with "buffer" as 0
* (i.e. flush) when the application terminates.
*/

size_t __write(int handle, const unsigned char *buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */
#if 1

  size_t nChars = 0;

  if (buffer == 0)
  {
    /*
     * This means that we should flush internal buffers.  Since we
     * don't we just return.  (Remember, "handle" == -1 means that all
     * handles should be flushed.)
     */
    return 0;
  }

  /* This template only writes to "standard out" and "standard err",
   * for all other file handles it returns failure. */
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (MyLowLevelPutchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }

    ++nChars;
  }

  return nChars;

#else

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;

#endif

}


